import http from "@/http";
import { categoryNames } from "@/datas/index";
import type { DiscoverAuthorType,SubtotalType, DiscoverSpecialType, DiscoverListType, DiscoverArticleType, SerializeBookDetailType, SerializeDetailType, HotAuthorDetailType, CommentType, SearchDetailType, HomeDateType, MonthArticlesDataType, ArticleDetailType, HpByDataType } from "@/typing";

export const Api = {
    // 首页文章
    getDateList: (date: string) => {
        return http.request<HomeDateType>({
            url: '/channel/one/' + date,
            method: 'get'
        })
    },
    // 首页下拉的月份列表
    getMonthArticles: (date: string) => {
        return http.request<MonthArticlesDataType>({
            url: '/feeds/list/' + date,
            method: 'get'
        })
    },
    // 图文
    getHpDetail: (date: string) => {
        return http.request<HpByDataType>({
            url: '/hp/bydate/' + date,
            method: 'get'
        })
    },
    // 文章详情 API
    getArticleDetail: (id: string, category: string) => {
        const categoryName = categoryNames[category]

        if (!categoryName) {
            console.error('未知的分类类型:', category)
            return
        }

        return http.request<ArticleDetailType>({
            url: `/${categoryName}/htmlcontent/${id}`,
            method: 'get'
        })
    },
    // 文章相关评论详情 API
    getArticleRatings: (id: string, category: string) => {
        const categoryName = categoryNames[category]

        if (!categoryName) {
            console.error('未知的分类类型:', category)
            return
        }

        return http.request<CommentType>({
            url: `/comment/praiseandtime/${categoryName}/${id}/0`,
            method: 'get'
        })
    },
    // 搜索接口
    getSearchDetail: (category: string, keyword: string, page: number) => {
        const categoryName = categoryNames[category]

        if (!categoryName) {
            console.error('未知的分类类型:', category)
            return
        }

        return http.request<SearchDetailType>({
            url: `search/${categoryName}/${keyword}/${page}`,
            method: 'get'
        })
    },
    // 热门作者
    getHotAuthorList: () => {
        return http.request<HotAuthorDetailType>({
            url: '/author/hot',
            method: 'get'
        })
    },
    // 作者详情
    getAuthorDetailList: (id:number) => {
        
        return http.request<DiscoverAuthorType>({
            url: `/author/works?author_id=${id}`,
            method: 'get'
        })
    },

    // 连载年份
    getSerialYearList: (year: number) => {
        return http.request<SerializeDetailType>({
            url: '/find/serial/byyear/' + year,
            method: 'get'
        })
    },
    // 连载列表
    getSerialBooksList: (serialId: string) => {

        return http.request<SerializeBookDetailType>({
            url: `/find/serial/list/${serialId}`,
            method: 'get'
        })
    },
    // 发现：文章列表
    getDiscoverArticleList: (category: number, month: string) => {
        return http.request<DiscoverArticleType>({
            url: `/find/bymonth/${category}/${month}`,
            method: 'get'
        })
    },
    // 发现：榜单
    getDiscoverList: () => {
        return http.request<DiscoverListType>({
            url: `/find/rank`,
            method: 'get'
        })
    },
    // 发现：专题
    getDiscoverSpecial: () => {
        return http.request<DiscoverSpecialType>({
            url: `/banner/list/4`,
            method: 'get'
        })
    },
    // 发现：小记
    getDiscoverSubtotal: (id: number) => {
        return http.request<SubtotalType>({
            url: `/diary/square/more/${id}`,
            method: 'get'
        })
    },
}
